﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    class Patient
    {
        protected float TTL; //Time to Live
        protected float maxTTL;
        public Vector2 UnitLoc;
        protected Texture2D texture;
        protected int unitSize = 64;
        protected Color colorCode;
        protected Texture2D healthbarTex;
        protected int mark;
        float dtime;
        public Boolean dead;
        public Boolean cured;

        public Patient(float X, float Y, float TTL)
        {
            UnitLoc = new Vector2(X, Y);
            texture = ResourceManager.getTexture("patient");
            this.maxTTL = TTL;
            this.TTL = TTL;
            dtime = 0;
            colorCode = Color.White;
            healthbarTex = ResourceManager.getTexture("plain");
            mark = 0;
            cured = false;

            if (TTL <= 0)
            {
                dead = true;
            }
            else
            {
                dead = false;
            }
        }

        public void curePatient()
        {
            cured = true;
            colorCode = Color.White;
            texture = ResourceManager.getTexture("patientCured");
        }

        public bool isMarked()
        {
            return mark != 0;
        }

        public void markPatient()
        {
            mark = 1;
            if (TTL < 30 && TTL > 0)
            {
                colorCode = Color.Red;
            }
            else if (TTL < 60)
            {
                colorCode = Color.Orange;
            }
            else if (TTL > 60)
            {
                colorCode = Color.Green;
            }
            else
            {
                colorCode = Color.Black;
            }
        }

        public void Update(GameTime gameTime)
        {
            if (TTL < 3)
            {
                dead = true;
            }
            else if (dtime == 0)
            {
                dtime = gameTime.TotalGameTime.Seconds;
            }
            else if ((gameTime.TotalGameTime.Seconds - dtime) > 1)
            {
                dtime = gameTime.TotalGameTime.Seconds;
                TTL--;
            }

            if (isMarked()&&dead)
            {
                colorCode = Color.Black;
            }
        }

        public  void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture,
                    new Rectangle(
                        (int)(UnitLoc.X) - unitSize / 2,
                        (int)(UnitLoc.Y) - unitSize / 2,
                        unitSize,
                        unitSize),
                    colorCode);
            if (isMarked()&&!dead&&!cured)
            {
                spriteBatch.Draw(healthbarTex,
                new Rectangle((int)(UnitLoc.X - unitSize / 2),
                    (int)(UnitLoc.Y - unitSize / 2) - 5,
                    (int)(((float)TTL / (float)maxTTL) * 64),
                    10),
                    Color.Green);
                spriteBatch.Draw(healthbarTex,
                    new Rectangle((int)((UnitLoc.X - unitSize / 2) + (((float)TTL / (float)maxTTL) * 64)),
                        (int)(UnitLoc.Y - unitSize / 2) - 5,
                        (int)(((1.0f - ((float)TTL / (float)maxTTL)) * 64)),
                        10),
                        Color.Black);
            }
        }
    }
}
